/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.AnsatController;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Jeppe
 */
public class LoginPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;

    /**
     * Creates new form LoginPanel
     */
    public LoginPanel(MainGUI mainGUI) {
        initComponents();
        this.mainGUI = mainGUI;
    }

    public void setScreenShotBaggrund(ImageIcon imgIcon) {
        screenShotBaggrundLabel.setIcon(imgIcon);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        loginKnap = new javax.swing.JButton();
        kodeordLabel = new javax.swing.JLabel();
        brugerLabel = new javax.swing.JLabel();
        passwordTxt = new javax.swing.JPasswordField();
        brugerTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        borderLabel = new javax.swing.JLabel();
        screenShotBaggrundLabel = new javax.swing.JLabel();

        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
        jPanel3.setOpaque(false);
        jPanel3.setLayout(null);

        jPanel2.setOpaque(false);
        jPanel2.setLayout(null);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Brugernavn: LYK");

        jLabel2.setText("Kodeord: 9007 ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel1);
        jPanel1.setBounds(88, 11, 127, 62);

        statusLabel.setBackground(new java.awt.Color(204, 204, 204));
        statusLabel.setForeground(new java.awt.Color(255, 0, 0));
        jPanel2.add(statusLabel);
        statusLabel.setBounds(30, 163, 240, 20);

        loginKnap.setText("Log ind");
        loginKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginKnapActionPerformed(evt);
            }
        });
        jPanel2.add(loginKnap);
        loginKnap.setBounds(158, 190, 90, 30);

        kodeordLabel.setText("Kodeord:");
        jPanel2.add(kodeordLabel);
        kodeordLabel.setBounds(26, 139, 60, 14);

        brugerLabel.setText("Brugernavn:");
        jPanel2.add(brugerLabel);
        brugerLabel.setBounds(10, 108, 70, 14);

        passwordTxt.setText("9007");
        passwordTxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                passwordTxtFocusGained(evt);
            }
        });
        passwordTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                passwordTxtKeyReleased(evt);
            }
        });
        jPanel2.add(passwordTxt);
        passwordTxt.setBounds(88, 136, 160, 23);

        brugerTxt.setText("LYK");
        brugerTxt.setNextFocusableComponent(passwordTxt);
        brugerTxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                brugerTxtFocusGained(evt);
            }
        });
        brugerTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                brugerTxtKeyPressed(evt);
            }
        });
        jPanel2.add(brugerTxt);
        brugerTxt.setBounds(88, 105, 160, 23);

        jPanel3.add(jPanel2);
        jPanel2.setBounds(60, 40, 310, 230);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrundOverlay.png"))); // NOI18N
        jPanel3.add(jLabel3);
        jLabel3.setBounds(1, 1, 418, 288);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrundOverlay.png"))); // NOI18N
        jLabel4.setText("jLabel4");
        jPanel3.add(jLabel4);
        jLabel4.setBounds(0, 0, 420, 290);

        add(jPanel3);
        jPanel3.setBounds(180, 120, 420, 290);

        borderLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
        add(borderLabel);
        borderLabel.setBounds(177, 117, 426, 296);
        add(screenShotBaggrundLabel);
        screenShotBaggrundLabel.setBounds(0, 0, 800, 610);
    }// </editor-fold>//GEN-END:initComponents

    private void loginKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginKnapActionPerformed
        try {
            if (AnsatController.getInstance().login(brugerTxt.getText(), passwordTxt.getText())) {
                mainGUI.showPage("MenuPanel");
                statusLabel.setText("");
                setVisible(false);
                mainGUI.updateBanner();
            } else {
                statusLabel.setText("Brugernavn og adgangskode er forkert.");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1006 \nFejl ved indlæsning af ansat. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "DatabaseTimeout", 0);
        }
    }//GEN-LAST:event_loginKnapActionPerformed

    private void brugerTxtKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_brugerTxtKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                if (AnsatController.getInstance().login(brugerTxt.getText(), passwordTxt.getText())) {
                    mainGUI.showPage("MenuPanel");
                    statusLabel.setText("");
                    setVisible(false);
                    mainGUI.updateBanner();
                } else {
                    statusLabel.setText("Brugernavn og adgangskode er forkert.");
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Fejl 1006 \nFejl ved indlæsning af ansat. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "DatabaseTimeout", 0);
            }
        }
    }//GEN-LAST:event_brugerTxtKeyPressed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        brugerTxt.requestFocus();
        brugerTxt.selectAll();
    }//GEN-LAST:event_formComponentShown

    private void passwordTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordTxtKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                if (AnsatController.getInstance().login(brugerTxt.getText(), passwordTxt.getText())) {
                    mainGUI.showPage("MenuPanel");
                    statusLabel.setText("");
                    setVisible(false);
                    mainGUI.updateBanner();
                } else {
                    statusLabel.setText("Brugernavn og adgangskode er forkert.");
                }
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "Fejl 1006 \nFejl ved indlæsning af ansat. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "DatabaseTimeout", 0);
            }
        }
    }//GEN-LAST:event_passwordTxtKeyReleased

    private void brugerTxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_brugerTxtFocusGained
        brugerTxt.selectAll();
    }//GEN-LAST:event_brugerTxtFocusGained

    private void passwordTxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_passwordTxtFocusGained
        passwordTxt.selectAll();
    }//GEN-LAST:event_passwordTxtFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel borderLabel;
    private javax.swing.JLabel brugerLabel;
    private javax.swing.JTextField brugerTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel kodeordLabel;
    private javax.swing.JButton loginKnap;
    private javax.swing.JPasswordField passwordTxt;
    private javax.swing.JLabel screenShotBaggrundLabel;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
}
